<?php

namespace App\Console;

use App\Events\OrderEvent;
use App\Library\User\TreeManager;
use App\Models\Order;
use App\WeChat\WechatDefaultConfig;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

class Kernel extends ConsoleKernel
{
    use WechatDefaultConfig;
    /**
     * The Artisan commands provided by your application.
     *
     * @var array
     */
    protected $commands = [
        //
    ];

    /**
     * Define the application's command schedule.
     *
     * @param  \Illuminate\Console\Scheduling\Schedule  $schedule
     * @return void
     */
    protected function schedule(Schedule $schedule)
    {
        // $schedule->command('inspire')
        //          ->hourly();

        $schedule->call(function (){
            //超过24小时订单自动关闭
            Order::with('trade')->where('trade_status', 1)
                ->where('created_at', '<', strtotime('-1 day'))
                ->get()->map(function (Order $order){
                    $order->update([
                        'trade_status'=>6,
                        'closed'=>1,
                        'closed_at'=>time(),
                        'updated_at'=>time()
                    ]);

                    if ($order->trade)
                    {
                        $order->trade->update([
                            'trade_status'=>6,
                            'updated_at'=>time()
                        ]);
                    }
                    event(new OrderEvent($order, 'closed'));
                });

            //超过10天的订单自动收货
            Order::with('trade')->where('trade_status', 3)
                ->where('created_at', '<', strtotime('-10 day'))
                ->get()->map(function (Order $order){
                    //货到付款
                    $order->update([
                        'trade_status'=>4,
                        'receive_status'=>1,
                        'receive_at'=>time(),
                        'updated_at'=>time()
                    ]);

                    $trade = $order->trade;
                    if ($trade)
                    {
                        $trade->update([
                            'trade_status'=>4,
                            'updated_at'=>time()
                        ]);
                        //卖增加余额
                        $sellerWallet = $order->seller->wallet()->firstOrCreate([]);
                        $sellerWallet->balance += $trade->total_amount;
                        $sellerWallet->total_income += $trade->total_amount;
                        $sellerWallet->save();
                        //买家扣款
                        $buyerWallet = $order->buyer->wallet()->firstOrCreate([]);
                        $buyerWallet->total_cost+= $trade->total_amount;
                        $buyerWallet->save();
                    } else {
                        //买家扣款
                        $buyerWallet = $order->buyer->wallet()->firstOrCreate([]);
                        $buyerWallet->total_cost+= $order->total_fee;
                        $buyerWallet->save();
                    }

                    event(new OrderEvent($order, 'received'));
                });

        })->everyTenMinutes();

        //每小时释放1次气泡
        $schedule->call(function (){
            TreeManager::releaseBubbles();
        })->hourly();
    }

    /**
     * Register the commands for the application.
     *
     * @return void
     */
    protected function commands()
    {
        $this->load(__DIR__.'/Commands');

        require base_path('routes/console.php');
    }
}
